import image
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QDialog,
    QFrame,
    QWidget,
    QVBoxLayout,
    QHBoxLayout,
    QLabel,
    QToolBar,
    QStyle,
    QStyleOption,
    QTableWidget,
    QTableWidgetItem,
    QSizePolicy,
    QMessageBox,
)
from PySide6.QtGui import QIcon, QPixmap, QBrush, QAction, QColor, QClipboard
from PySide6.QtCore import Qt, QSize, QTimer, Signal, QEvent


class MainChangeWindow(QWidget):
    soap = Signal(list)

    def __init__(self, parent=None):
        super(MainChangeWindow, self).__init__(parent)
        self.tool = apartment()
        self.room = farmland()
        h = QHBoxLayout(self)
        h.setContentsMargins(0, 0, 0, 0)
        h.addWidget(self.room)
        h.addWidget(self.tool)
        # 信号槽
        self.tool.actionTriggered.connect(self.MSG_SYSTEM)

    def MSG_SYSTEM(self, e):
        """toolbar事件"""
        # print(e.text())
        if e.text() == "克隆到左边":
            for row in range(22):
                for col in range(11, 22):
                    if (row, col) in self.room.takeup:
                        continue
                    d = self.room.item(row, col)
                    t = self.room.item(row, 21 - col)
                    t.setBackground(d.background())
                    t.setForeground(d.background())
                    # t.setText(d.text())
        elif e.text() == "克隆到右边":
            for row in range(22):
                for col in range(11):
                    if (row, col) in self.room.takeup:
                        continue
                    d = self.room.item(row, col)
                    t = self.room.item(row, 21 - col)
                    t.setBackground(d.background())
                    t.setForeground(d.background())
                    # t.setText(d.text())
        elif e.text() == "生成数据":
            data = [
                [
                    self.tool.hasIndex(self.room.item(row, col).background())
                    for col in range(22)
                ]
                for row in range(22)
            ]
            # print(data)
            self.soap.emit(data)
        elif e.text() == "初始化地图":
            self.room.setDefaultData()
        else:
            obj = self.tool.hasAttr(e.text())
            for black in self.room.selectedItems():
                x, y = self.room.row(black), self.room.column(black)
                if (x, y) in self.room.takeup:
                    continue
                black.setBackground(obj["color"])
                black.setForeground(obj["color"])
                # black.setText(f'{obj["idx"]}')


class apartment(QToolBar):
    """工具间"""

    def __init__(self, parent=None):
        super(apartment, self).__init__(parent)
        self.setIconSize(QSize(32, 32))
        self.setOrientation(Qt.Vertical)
        self.legend = {
            "流沙地形 低速": {"idx": 0, "color": QColor(238, 194, 17)},
            "普通地形 标准": {"idx": 1, "color": QColor(179, 179, 77)},
            "高速公路 高速": {"idx": 2, "color": QColor(160, 160, 160)},
            "丛林道路 隐蔽": {"idx": 3, "color": QColor(22, 175, 77)},
            "普通障碍 轻度毁伤": {"idx": 4, "color": QColor(195, 137, 77)},
            "坚硬障碍 中度毁伤": {"idx": 5, "color": QColor(225, 107, 77)},
            "牢固障碍 重度毁伤": {"idx": 6, "color": QColor(255, 77, 77)},
            "水面障碍 无敌，穿透": {"idx": 7, "color": QColor(51, 79, 180)},
            "无敌障碍 无敌": {"idx": 8, "color": QColor(250, 250, 250)},
            # "基地": {"idx": 9, "color": QColor(18, 23, 21)},
        }
        self.defaultAction()

    def defaultAction(self):
        for name, args in self.legend.items():
            icon = QPixmap(24, 24)
            icon.fill(args["color"])
            self.addAction(QAction(QIcon(icon), name, self))
            if args["idx"] in [3, 6]:
                self.addSeparator()
        self.addSeparator()
        self.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.StandardPixmap(54)),
                "克隆到左边",
                self,
            )
        )
        self.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.StandardPixmap(55)),
                "克隆到右边",
                self,
            )
        )
        self.addSeparator()
        self.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.StandardPixmap(36)),
                "生成数据",
                self,
            )
        )
        self.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.StandardPixmap(59)),
                "初始化地图",
                self,
            )
        )

    def hasAttr(self, name):
        """返回对象"""
        return self.legend.get(name)

    def hasIndex(self, color):
        """根据颜色返回索引"""
        for obj in self.legend.values():
            # print(color, QBrush(obj["color"]))
            if QBrush(obj["color"]) == color:
                return obj["idx"]
        return 9


class farmland(QTableWidget):
    """作业车间"""

    def __init__(self, parent=None):
        super(farmland, self).__init__(parent)
        self.setRowCount(22)
        self.setColumnCount(22)
        # self.horizontalHeader().hide()
        # self.verticalHeader().hide()
        self.setHorizontalHeaderLabels([str(x) for x in range(22)])
        self.setVerticalHeaderLabels([str(x) for x in range(22)])
        self.verticalHeader().setDisabled(True)  # 不让用户改行高
        self.horizontalHeader().setDisabled(True)  # 不让用户改列宽
        self.setWidth()
        self.setHeight()
        self.setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding
        )  # 设置尺寸策略为自动扩展
        self.setDefaultData()
        # 预留位置
        self.takeup = [
            (0, 0),
            (0, 1),
            (1, 0),
            (1, 1),
            (0, 10),
            (1, 10),
            (0, 11),
            (1, 11),
            (0, 20),
            (1, 20),
            (0, 21),
            (1, 21),
            (20, 10),
            (21, 10),
            (20, 11),
            (21, 11),
            (21, 9),
            (20, 9),
            (19, 9),
            (19, 10),
            (19, 11),
            (19, 12),
            (20, 12),
            (21, 12),
            (20, 7),
            (21, 7),
            (20, 8),
            (21, 8),
            (20, 13),
            (21, 13),
            (20, 14),
            (21, 14),
        ]

    def setWidth(self):
        """设置列宽"""
        for i in range(self.columnCount()):
            self.setColumnWidth(i, 30)

    def setHeight(self):
        """设置行高"""
        for i in range(self.rowCount()):
            self.setRowHeight(i, 30)

    def setDefaultData(self):
        """默认值"""
        for row in range(self.rowCount()):
            for column in range(self.columnCount()):
                item = QTableWidgetItem()
                # item.setText("1")
                item.setBackground(QColor(179, 179, 77))
                item.setForeground(QColor(179, 179, 77, 255))
                item.setTextAlignment(Qt.AlignCenter)
                self.setItem(row, column, item)

        # self.item(0, 0).setText("1")
        # self.item(0, 1).setText("1")
        # self.item(1, 0).setText("1")
        # self.item(1, 1).setText("1")
        # self.item(0, 0).setBackground(QColor(5, 5, 5))
        # self.item(0, 1).setBackground(QColor(5, 5, 5))
        # self.item(1, 0).setBackground(QColor(5, 5, 5))
        # self.item(1, 1).setBackground(QColor(5, 5, 5))
        # self.item(0, 0).setForeground(QColor(5, 5, 5))
        # self.item(0, 1).setForeground(QColor(5, 5, 5))
        # self.item(1, 0).setForeground(QColor(5, 5, 5))
        # self.item(1, 1).setForeground(QColor(5, 5, 5))

        # self.item(0, 10).setText("1")
        # self.item(1, 10).setText("1")
        # self.item(0, 11).setText("1")
        # self.item(1, 11).setText("1")
        # self.item(0, 10).setBackground(QColor(5, 5, 5))
        # self.item(1, 10).setBackground(QColor(5, 5, 5))
        # self.item(0, 11).setBackground(QColor(5, 5, 5))
        # self.item(1, 11).setBackground(QColor(5, 5, 5))
        # self.item(0, 10).setForeground(QColor(5, 5, 5))
        # self.item(1, 10).setForeground(QColor(5, 5, 5))
        # self.item(0, 11).setForeground(QColor(5, 5, 5))
        # self.item(1, 11).setForeground(QColor(5, 5, 5))

        # self.item(0, 20).setText("1")
        # self.item(1, 20).setText("1")
        # self.item(0, 21).setText("1")
        # self.item(1, 21).setText("1")
        # self.item(0, 20).setBackground(QColor(5, 5, 5))
        # self.item(1, 20).setBackground(QColor(5, 5, 5))
        # self.item(0, 21).setBackground(QColor(5, 5, 5))
        # self.item(1, 21).setBackground(QColor(5, 5, 5))
        # self.item(0, 20).setForeground(QColor(5, 5, 5))
        # self.item(1, 20).setForeground(QColor(5, 5, 5))
        # self.item(0, 21).setForeground(QColor(5, 5, 5))
        # self.item(1, 21).setForeground(QColor(5, 5, 5))

        # self.item(20, 10).setText("9")
        # self.item(21, 10).setText("9")
        # self.item(20, 11).setText("9")
        # self.item(21, 11).setText("9")
        # self.item(20, 10).setBackground(QColor(5, 5, 5))
        # self.item(21, 10).setBackground(QColor(5, 5, 5))
        # self.item(20, 11).setBackground(QColor(5, 5, 5))
        # self.item(21, 11).setBackground(QColor(5, 5, 5))
        # self.item(20, 10).setForeground(QColor(5, 5, 5))
        # self.item(21, 10).setForeground(QColor(5, 5, 5))
        # self.item(20, 11).setForeground(QColor(5, 5, 5))
        # self.item(21, 11).setForeground(QColor(5, 5, 5))

        # self.item(21, 9).setText("4")
        # self.item(20, 9).setText("4")
        # self.item(19, 9).setText("4")
        # self.item(19, 10).setText("4")
        # self.item(19, 11).setText("4")
        # self.item(19, 12).setText("4")
        # self.item(20, 12).setText("4")
        # self.item(21, 12).setText("4")
        self.item(21, 9).setBackground(QColor(195, 137, 77))
        self.item(20, 9).setBackground(QColor(195, 137, 77))
        self.item(19, 9).setBackground(QColor(195, 137, 77))
        self.item(19, 10).setBackground(QColor(195, 137, 77))
        self.item(19, 11).setBackground(QColor(195, 137, 77))
        self.item(19, 12).setBackground(QColor(195, 137, 77))
        self.item(20, 12).setBackground(QColor(195, 137, 77))
        self.item(21, 12).setBackground(QColor(195, 137, 77))
        # self.item(21, 9).setForeground(QColor(195, 137, 77))
        # self.item(20, 9).setForeground(QColor(195, 137, 77))
        # self.item(19, 9).setForeground(QColor(195, 137, 77))
        # self.item(19, 10).setForeground(QColor(195, 137, 77))
        # self.item(19, 11).setForeground(QColor(195, 137, 77))
        # self.item(19, 12).setForeground(QColor(195, 137, 77))
        # self.item(20, 12).setForeground(QColor(195, 137, 77))
        # self.item(21, 12).setForeground(QColor(195, 137, 77))

        # self.item(20, 7).setText("1")
        # self.item(21, 7).setText("1")
        # self.item(20, 8).setText("1")
        # self.item(21, 8).setText("1")
        # self.item(20, 7).setBackground(QColor(5, 5, 5))
        # self.item(21, 7).setBackground(QColor(5, 5, 5))
        # self.item(20, 8).setBackground(QColor(5, 5, 5))
        # self.item(21, 8).setBackground(QColor(5, 5, 5))
        # self.item(20, 7).setForeground(QColor(5, 5, 5))
        # self.item(21, 7).setForeground(QColor(5, 5, 5))
        # self.item(20, 8).setForeground(QColor(5, 5, 5))
        # self.item(21, 8).setForeground(QColor(5, 5, 5))

        # self.item(20, 13).setText("1")
        # self.item(21, 13).setText("1")
        # self.item(20, 14).setText("1")
        # self.item(21, 14).setText("1")
        # self.item(20, 13).setBackground(QColor(5, 5, 5))
        # self.item(21, 13).setBackground(QColor(5, 5, 5))
        # self.item(20, 14).setBackground(QColor(5, 5, 5))
        # self.item(21, 14).setBackground(QColor(5, 5, 5))
        # self.item(20, 13).setForeground(QColor(5, 5, 5))
        # self.item(21, 13).setForeground(QColor(5, 5, 5))
        # self.item(20, 14).setForeground(QColor(5, 5, 5))
        # self.item(21, 14).setForeground(QColor(5, 5, 5))
